//
//  WebViewController.m
//  qq
//
//  Created by 王成龙 on 2016/11/21.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *web;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *av;



@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//self.title = @"百度";
    //搜索栏
    UISearchBar *searchBar = [UISearchBar new];
    searchBar.delegate = self;
    searchBar.returnKeyType = UIReturnKeySearch;
    searchBar.keyboardType = UIKeyboardTypeURL;//有利于输入
    self.navigationItem.titleView = searchBar;//添加搜索框
    
    
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    self.web.delegate = self;
    [self.web loadRequest:req];
    [self.av startAnimating];
    //工具栏放东西
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(onback:)];
    UIBarButtonItem *forward = [[UIBarButtonItem alloc]initWithTitle:@">" style:UIBarButtonItemStylePlain target:self action:@selector(onforward:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    self.toolbarItems = @[back,forward,space];
}
-(void)onback:(id)sender
{
    if([self.web canGoBack])
    {
        [self.web goBack];
    }
}
-(void)onforward:(id)sender
{
    if([self.web canGoForward])
    {
        [self.web goForward];
    }
}
//加载结束
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.av stopAnimating];
}
//工具栏

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
}

- (void)viewDidDisappear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    NSLog(@"%@",searchBar.text);
    NSString *str = searchBar.text;
    if(![str hasPrefix:@"http://"])
    {
        str = [@"http://"stringByAppendingString:str];
    }
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:req];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
